<?php

namespace App\Http\Controllers\Cruise\Web;

use App\Http\Controllers\BaseController;
use App\Models\Cruise\CruiseTemplateModel;
use App\Service\Cruise\Check\CruiseCheckSpecialService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
 * 专项巡检任务
 */
class CruiseCheckSpecialController extends BaseController
{
    public function lists(Request $request): JsonResponse
    {
        $name         = (string)$request->get('name');
        $status       = (int)$request->get('status');
        $pageSize     = (int)$request->get('pageSize', 20);
        $pageNum      = (int)$request->get('pageNum', 1);
        $templateType = (int)$request->get('templateType', CruiseTemplateModel::TYPE_3);
        $shopId       = (int)$request->get('shopId');
        $checkType    = (int)$request->get('checkType', -1);
        $export       = (int)$request->get("export", 0);

        return $this->success(CruiseCheckSpecialService::lists(
            $name, $status, $pageSize, $pageNum, $templateType,
            $shopId, $checkType, $export));
    }

    public function push(Request $request, int $id): JsonResponse
    {
        CruiseCheckSpecialService::push($id);

        return $this->success();
    }

    public function offline(Request $request, int $id): JsonResponse
    {
        $userId = getRmsUserId();
        CruiseCheckSpecialService::offline($id, $userId);

        return $this->success();
    }

    public function detail(Request $request, int $id): JsonResponse
    {
        return $this->success(CruiseCheckSpecialService::detail($id));
    }

    public function completion(Request $request, int $id): JsonResponse
    {
        return $this->success(CruiseCheckSpecialService::completion($id));
    }

    public function add(Request $request): JsonResponse
    {
        $name          = (string)$request->get('name');
        $startDateTime = (string)$request->get('startDateTime');
        $endDateTime   = (string)$request->get('endDateTime');
        $templateId    = (int)$request->get('templateId');
        $groups        = (array)$request->get('groups');

        $this->validate($request, [
            'name'                          => 'required|string',
            'startDateTime'                 => 'required|date',
            'endDateTime'                   => 'required|date',
            'templateId'                    => 'required|integer',
            'groups'                        => 'required|array',
            'groups.*.role'                 => 'required|integer',
            'groups.*.userId'               => 'required_if:role,1|integer',
            'groups.*.companyId'            => 'required_if:role,2|string',
        ]);

        $templateType = CruiseTemplateModel::query()->where('id', $templateId)->value('type');
        if ($templateType == CruiseTemplateModel::TYPE_3) {
            $this->validate($request, [
                'groups.*.shopIds'              => 'required|array',
            ]);
        } elseif ($templateType == CruiseTemplateModel::TYPE_5) {
            $this->validate($request, [
                'groups.*.cruiseCheckCompanyIds' => 'required|array',
            ]);
        }

        $id = CruiseCheckSpecialService::add($name, strtotime($startDateTime), strtotime($endDateTime . ' 23:59:59'),
            $templateId, $groups);

        return $this->success(['id' => $id]);
    }

    public function edit(Request $request, int $id): JsonResponse
    {
        $name          = (string)$request->get('name');
        $startDateTime = (string)$request->get('startDateTime');
        $endDateTime   = (string)$request->get('endDateTime');
        $templateId    = (int)$request->get('templateId');
        $groups        = (array)$request->get('groups');

        $request = $request->merge(['id' => $id]);

        $this->validate($request, [
            'id'                 => 'required|integer',
            'name'               => 'required|string',
            'startDateTime'      => 'required|date',
            'endDateTime'        => 'required|date',
            'templateId'         => 'required|integer',
            'groups'             => 'required|array',
            'groups.*.role'      => 'required|integer',
            'groups.*.userId'    => 'required_if:role,1|integer',
            'groups.*.companyId' => 'required_if:role,2|string',
        ]);

        $templateType = CruiseTemplateModel::query()->where('id', $templateId)->value('type');
        if ($templateType == CruiseTemplateModel::TYPE_3) {
            $this->validate($request, [
                'groups.*.shopIds'              => 'required|array',
            ]);
        } elseif ($templateType == CruiseTemplateModel::TYPE_5) {
            $this->validate($request, [
                'groups.*.cruiseCheckCompanyIds' => 'required|array',
            ]);
        }

        CruiseCheckSpecialService::edit($id, $name, strtotime($startDateTime), strtotime($endDateTime . ' 23:59:59'),
            $templateId, $groups);

        return $this->success();
    }

    public function addSelects(Request $request)
    {
        $templateType = (int)$request->get('templateType', CruiseTemplateModel::TYPE_3);

        return $this->success(CruiseCheckSpecialService::addSelects($templateType));
    }
}
